#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define M1 1000
#define N1 10
#define M2 100
#define N2 100
#define M3 10
#define N3 1000
#define T 10000

clock_t help_ProgramA_rows(int M, int N) {
    int i, j, *a;

    // Allocate memory for the array
    a = (int *)malloc(M * N * sizeof(int));

    // Measure execution time
    clock_t start_time = clock();

    // Initialize array
    for (i = 0; i < M; i++)
        for (j = 0; j < N; j++)
            a[i + j * M] = i + j;

    // Main program segment with additional computation
    for (i = 0; i < M; i++)
        for (j = 0; j < N; j++)
            a[i + j * M] = a[i + j * M] + i * j;

    clock_t end_time = clock();
    free(a);
    return end_time - start_time;
}
clock_t help_ProgramB_cols(int M, int N) {
    int i, j, *a;

    // Allocate memory for the array
    a = (int *)malloc(M * N * sizeof(int));

    // Measure execution time
    clock_t start_time = clock();

    // Initialize array
    for (i = 0; i < M; i++)
        for (j = 0; j < N; j++)
            a[j * M + i] = i + j;

    // Main program segment with additional computation
    for (j = 0; j < N; j++)
        for (i = 0; i < M; i++)
            a[j * M + i] = a[j * M + i] + i * j;

    clock_t end_time = clock();
    free(a);
    return end_time - start_time;
}
void ProgramA_rows(int M, int N) {
    clock_t execution_time = 0;
    for (int t = 0; t < T; ++t) {
        execution_time += help_ProgramA_rows(M, N);
    }
    printf("Execution time for ProgramA_rows(): %lf microseconds\n", execution_time / (double)T);
}
void ProgramB_cols(int M, int N) {
    clock_t execution_time = 0;
    for (int t = 0; t < T; ++t) {
        execution_time += help_ProgramB_cols(M, N);
    }
    printf("Execution time for ProgramB_cols(): %lf microseconds\n", execution_time / (double)T);
}
int main() {
    int Ms[3] = { M1,M2,M3 };
    int Ns[3] = { N1,N2,N3 };
    for (int i = 0; i < 3; ++i) {
        ProgramA_rows(Ms[i], Ns[i]);
        ProgramB_cols(Ms[i], Ns[i]);
    }

    return 0;
}
